﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Evolution.Structure;
using System.Xml;
using System.Windows;
using System.Windows.Input;

namespace GameProject.ViewModel
{
    class PlayerNameViewModel : ViewModelBase
    {
            private MainWindowViewModel mainWindowViewModel;


         /*
         * Create BindingList for automatically update the Playlist
         */ 
        public BindingList<Player> PlayerList
        {
            get
            {
                return _PlayerList;
            }
            set
            {
                _PlayerList = value;
                base.onPropertyChanged("PlayerList");
            }
        }
        private BindingList<Player> _PlayerList;
        XmlDocument doc = new XmlDocument();

        /*
         * Constructor
         */
        public PlayerNameViewModel(MainWindowViewModel mainWindowVM)
        {            
            mainWindowViewModel = mainWindowVM;

            if (_PlayerList == null) _PlayerList = new BindingList<Player>();
            try
            {
                doc.Load(@"xml\players.xml");
                XmlNodeList playerXmlList = doc.SelectNodes("/Players/Player/Name");
                foreach (XmlNode node in playerXmlList)
                {
                    _PlayerList.Add(new Player(node.InnerText));
                }
            }
            catch(Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
        /*
         * Constructor
         * END
         */

        /*
         * Create Player
         */
        public ICommand CreatePlayerCommand
        {
            get
            {
                if (_CreatePlayerCommand == null)
                {
                    _CreatePlayerCommand = new RelayCommand(param => createPlayer(), param => checkCreatePlayer());
                }
                return _CreatePlayerCommand;
            }
        }
        private ICommand _CreatePlayerCommand;

        private bool checkCreatePlayer()
        {
            if (NewPlayerName == null) return false;
            if (NewPlayerName == "") return false;

            foreach (Player p in PlayerList)
            {
                if (String.Equals(p.Name, NewPlayerName, StringComparison.CurrentCultureIgnoreCase))
                    return false;

            }
            
            return true;
        }

        public String NewPlayerName { get{
            return _NewPlayerName;
        }
            set
            {
                _NewPlayerName = value;
                base.onPropertyChanged("NewPlayerName");
            }
        }
        private String _NewPlayerName;

        private void createPlayer()
        {
            Player newPlayer = new Player(NewPlayerName);
            PlayerList.Add(newPlayer);

            XmlNode root = doc.DocumentElement;

            //Create a new node.
            XmlElement elementPlayer = doc.CreateElement("Player");
            XmlElement elementName = doc.CreateElement("Name");

            //Add the node to the document.
            root.AppendChild(elementPlayer).AppendChild(elementName).InnerText = NewPlayerName;

            doc.Save(@"xml\players.xml");

            SelectedPlayer = newPlayer;
        }
        /*
         * Create Player
         * END
         */

        /*
         * Selected Player in Listbox
         */
        public Player SelectedPlayer
        {
            get { return _SelectedPlayer; }
            set
            {
                _SelectedPlayer = value;                    
                base.onPropertyChanged("SelectedPlayer");
            }
        }
        private Player _SelectedPlayer;

        public ICommand SelectPlayerCommand
        {
            get
            {
                if (_SelectPlayerCommand == null)
                {
                    _SelectPlayerCommand = new RelayCommand(param => selectPlayer(), param => canSelectPlayer());
                }
                return _SelectPlayerCommand;
            }
        }
        private ICommand _SelectPlayerCommand;

        private Boolean canSelectPlayer()
        {
            if (SelectedPlayer == null)
                return false;
            else return true;
        }

        private void selectPlayer()
        {
            GlobalVar.CurrentPlayer = _SelectedPlayer;
            mainWindowViewModel.OpenGameMenu();
        }
        /*
         * Selected Player in Listbox
         * END
         */
    }
}
